package com.lbloca.locate.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import com.lbloca.locate.R;
import com.lbloca.locate.biz.Config;
import com.lbloca.locate.utils.SPUtils;


/**
 * 底部选择支付方式
 */

public class BottomPayDialog extends Dialog implements View.OnClickListener {
    private Activity mContext;
    private TextView mTitle;
    private TextView mAlipayInfo;
    private TextView mFavorable;
    private TextView mWeixinPay;
    private TextView mCancel;

    private RelativeLayout alipay_relativeLayout;

    public BottomPayDialog(@NonNull Activity context) {
        super(context);
        this.mContext = context;

    }

    public void setIOnClickListener(IOnClickListener iOnClickListener) {
        this.mIOnClickListener = iOnClickListener;
    }

    public BottomPayDialog(@NonNull Context context, @StyleRes int themeResId, Activity mContext) {
        super(context, themeResId);
        this.mContext = mContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vw_bottom_pay);
        init();
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.dialogstyle); // 添加动画
        WindowManager windowManager = mContext.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = display.getWidth(); //设置宽度
        dialogWindow.setAttributes(lp);
    }

    private void init() {
        mTitle = findViewById(R.id.title);
        mAlipayInfo = findViewById(R.id.alipay_info);
        mWeixinPay = findViewById(R.id.wexinpay);
        mCancel = findViewById(R.id.cancel);

        alipay_relativeLayout = findViewById(R.id.alipay);

        if (SPUtils.getInstance().getBoolean(Config.BTN_WECHAT))
            mWeixinPay.setVisibility(View.VISIBLE);
        else
            mWeixinPay.setVisibility(View.GONE);

        if (SPUtils.getInstance().getBoolean(Config.BTN_ALIPAY))
            alipay_relativeLayout.setVisibility(View.VISIBLE);
        else
            alipay_relativeLayout.setVisibility(View.GONE);
        mCancel.setOnClickListener(this);
        mAlipayInfo.setOnClickListener(this);
        mWeixinPay.setOnClickListener(this);
    }

    public void setTitle(int id) {
        mTitle.setText(id);
    }

    public void setFirstItem(int id) {
        mAlipayInfo.setText(id);
    }

    public void setSecondItem(int id) {
        mWeixinPay.setText(id);
    }

    public void setFavorableShow(int visibility) {
        mFavorable.setVisibility(visibility);
    }

    private IOnClickListener mIOnClickListener;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.alipay_info:
                if (mIOnClickListener != null) {
                    mIOnClickListener.onItemClick(0);
                }
                break;
            case R.id.wexinpay:
                if (mIOnClickListener != null) {
                    mIOnClickListener.onItemClick(1);
                }
                break;
            case R.id.cancel:
                dismiss();
        }
    }

    public interface IOnClickListener {
        void onItemClick(int position);
    }
}
